package com.yjc.drygoods.product.service.impl;

import com.yjc.common.to.SpuCommentCountTo;
import com.yjc.common.utils.PageUtils;
import com.yjc.drygoods.product.dao.SpuCommentDao;
import com.yjc.drygoods.product.entity.SkuInfoEntity;
import com.yjc.drygoods.product.entity.SpuCommentEntity;
import com.yjc.drygoods.product.service.SkuInfoService;
import com.yjc.drygoods.product.service.SpuCommentService;
import com.yjc.drygoods.product.vo.CommentStarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.Query;
import org.springframework.util.StringUtils;


@Service("spuCommentService")
public class SpuCommentServiceImpl extends ServiceImpl<SpuCommentDao, SpuCommentEntity> implements SpuCommentService {

    @Autowired
    SkuInfoService skuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<SpuCommentEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("sku_id", key).or().like("spu_name", key);
        }
        IPage<SpuCommentEntity> page = this.page(
                new Query<SpuCommentEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SpuCommentEntity> getAllSpuCommentInfo() {
        return this.list();
    }

    @Override
    public List<SpuCommentCountTo> getBatchSkuComments(List<Long> skuIds) {
        List<SpuCommentCountTo> spuCommentCounts = skuIds.stream().map(item -> {
            Integer count = this.baseMapper.selectCount(new QueryWrapper<SpuCommentEntity>().eq("sku_id", item));
            SpuCommentCountTo spuCommentCountTo = new SpuCommentCountTo();
            spuCommentCountTo.setSkuId(item);
            spuCommentCountTo.setSpuCommentCount(count);
            return spuCommentCountTo;
        }).collect(Collectors.toList());
        return spuCommentCounts;
    }

    @Override
    public CommentStarVo getSkuCommentStatus(String key) {
        CommentStarVo commentStarVo = new CommentStarVo();
        SkuInfoEntity skuInfo = skuInfoService.getOne(new QueryWrapper<SkuInfoEntity>().eq("sku_name", key));
        List<SpuCommentEntity> list = this.list(new QueryWrapper<SpuCommentEntity>()
                .eq("sku_id", skuInfo.getSkuId())
                .or()
                .eq("sku_id", key));
        if (list != null && list.size() > 0) {
            commentStarVo.setSkuId(list.get(0).getSkuId());
            commentStarVo.setSpuName(list.get(0).getSpuName());
            for (SpuCommentEntity spuComment : list) {
                switch (spuComment.getStar()) {
                    case 1:
                        commentStarVo.setOneStar(commentStarVo.getOneStar() + 1);
                        break;
                    case 2:
                        commentStarVo.setTwoStar(commentStarVo.getTwoStar() + 1);
                        break;
                    case 3:
                        commentStarVo.setThreeStar(commentStarVo.getThreeStar() + 1);
                        break;
                    case 4:
                        commentStarVo.setFourStar(commentStarVo.getFourStar() + 1);
                        break;
                    case 5:
                        commentStarVo.setFiveStar(commentStarVo.getFiveStar() + 1);
                        break;
                }
            }
        }
        return commentStarVo;
    }


}